var app = require("lzbjpk_app");

var LogicBJPKGame = app.BaseClass.extend({

	Init: function () {

		this.JS_Name = "LogicBJPKGame";

		this.ComTool = app[app.subGameName + "_ComTool"]();
		this.ShareDefine = app[app.subGameName + "_ShareDefine"]();
		this.BJPKDefine = app[app.subGameName.toUpperCase()+"Define"]();
		this.WeChatManager = app[app.subGameName + "_WeChatManager"]();
		this.PokerCard = app[app.subGameName.toUpperCase()+"PokerCard"]();
		this.HUA_LEN = 4;

		console.log("Init");

		//单张A最大牌，牌型是 A23最小，QKA是最大 
		this.pokerType = [
			0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,   //方块 2-A 2 - 14
			0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E,   //梅花 2-A 18 - 30
			0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E,   //红桃 2-A 34 - 46
			0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E,	//黑桃 2-A 50 - 62
		];	
		this.LOGIC_MASK_COLOR = 0xF0; //花色掩码
		this.LOGIC_MASK_VALUE = 0x0F; //牌值掩码

		this.Logic_xiaowang = 0x41;  //小王	65
		this.Logic_dawang = 0x42;  //大王 66

		//癞子数组
		this.LaiZis = [this.Logic_xiaowang,this.Logic_dawang];

		//花色
		this.cardColor_Block = 0; //方块
		this.cardColor_PlumBlossom = 16; //梅花
		this.cardColor_Heart = 32; //红桃
		this.cardColor_Spade = 48; //黑桃
	},

	GetCardType: function (bCardData, cbCardCount) {
	},
	sortFun: function (a, b) {
		return a - b;
	},
	CheckCardType: function (cards) {
		/*if(this.GetWuTong(cards).length != 0)
			return 10;
		else*/
		if (this.GetTongHuaShun(cards).length != 0) {
			return 9;
		}
		/*else if(this.GetZhaDang(cards).length != 0)
			return 8;*/
		/*else if(this.GetHulu(cards).length != 0)
			return 7;*/
		else if (this.GetLiangDuiTongHua(cards).length != 0) {
			return 6;
		} else if (this.GetYiDuiTongHua(cards).length != 0) {
			return 5;
		} else if (this.GetTonghuaEx(cards).length != 0) {
			return 4;
		} else if (this.GetShunziEx(cards).length != 0) {
			return 3;
		} else if (this.GetSanTiaoEx(cards, false).length != 0) {
			return 2;
		}
		/*else if(this.GetLiangDui(cards).length != 0)
			return 1;*/
		else if (this.GetDuiZi(cards).length != 0) {
			return 0;
		} else {
			return -1;
		}
	},

	CheckDuiziByGui: function (pokers, guiList, duizis) {
		let aList = [];
		for (let i = 0; i < pokers.length; i++) {
			if (guiList.indexOf(pokers[i]) != -1) {
				continue;
			}
			if (duizis[0]) {
				if (duizis[0].indexOf(pokers[i]) != -1) {
					continue;
				}
			}
			let obj = {};
			let value = this.GetCardValue(pokers[i]);
			obj.value = value;
			obj.valueX16 = pokers[i];
			aList.push(obj);
		}

		aList.sort(function (a, b) {
			return b.value - a.value;
		});

		if (aList.length && guiList.length) {
			let aDui = [];
			aDui.push(aList[0].valueX16);
			aDui.push(guiList[0]);
			duizis[duizis.length] = aDui;
		}
	},

	CheckCardBigOrSmall: function (aCards, bCards) {//必须满敦在比
		//return 0 aCards大 1相反 2一样大
		var tempSortAcards = [];
		var tempSortBcards = [];
		for (var i = 0; i < aCards.length; i++)
			tempSortAcards.push(this.GetCardValue(aCards[i]));
		for (var i = 0; i < bCards.length; i++)
			tempSortBcards.push(this.GetCardValue(bCards[i]));

		tempSortAcards.sort(this.sortFun);
		tempSortBcards.sort(this.sortFun);

		let aValue = -1;
		let bValue = -1;
		//先找出不带鬼的对子
		let duiziAs = this.GetDuiZi(aCards, false);
		let duiziBs = this.GetDuiZi(bCards, false);
		//先找出鬼牌
		let guiAs = this.GetGuiPai(aCards);
		let guiBs = this.GetGuiPai(bCards);

		////////////////////五同
		/*if(this.GetWuTong(aCards).length != 0){
			if(this.GetWuTong(bCards).length != 0){
				for(let i = 0; i < aCards.length; i++){
					if(guiAs.indexOf(aCards[i]) != -1) continue;
					aValue = this.GetCardValue(aCards[i]);
					break;
				}
				for(let i = 0; i < bCards.length; i++){
					if(guiBs.indexOf(bCards[i]) != -1) continue;
					bValue = this.GetCardValue(bCards[i]);
					break;
				}
				if(aValue > bValue)
					return 0;
				else if(aValue < bValue)
					return 1;
				else{
					if(aValue > bValue)
						return 0;
					else if(aValue < bValue)
						return 1;
					else{
						if(!guiAs.length && guiBs.length){
							return 1;
						}
						else if(guiAs.length && !guiBs.length){
							return 0;
						}
						else{
							return 2;
						}
					}
				}
			}
			else
				return 0;
		}
		else if(this.GetWuTong(bCards).length != 0) {
			return 1;
		}*/
		////////////////////同花顺
		if (this.GetTongHuaShun(aCards).length != 0) {
			if (this.CheckCardType(bCards) > 9) {
				return 1;
			} else if (this.CheckCardType(bCards) == 9) {
				let aList = [];
				let bList = [];

				for (let i = 0; i < aCards.length; i++) {
					if (guiAs.indexOf(aCards[i]) != -1) {
						continue;
					}
					let value = this.GetCardValue(aCards[i]);
					aList.push(value);
				}

				for (let i = 0; i < bCards.length; i++) {
					if (guiBs.indexOf(bCards[i]) != -1) {
						continue;
					}
					let value = this.GetCardValue(bCards[i]);
					bList.push(value);
				}

				aList.sort(this.sortFun);
				bList.sort(this.sortFun);

				tempSortAcards = this.GetCompleteShun(aList);
				tempSortBcards = this.GetCompleteShun(bList);

				aValue = tempSortAcards[tempSortAcards.length - 1];
				bValue = tempSortBcards[tempSortBcards.length - 1];
				if (aValue > bValue) {
					return 0;
				} else if (aValue < bValue) {
					return 1;
				} else {//要判断下A
					if (14 == aValue && 14 == bValue) {
						aValue = tempSortAcards[tempSortAcards.length - 2];
						bValue = tempSortBcards[tempSortBcards.length - 2];
						if (aValue > bValue) {
							return 0;
						} else if (aValue < bValue) {
							return 1;
						} else {
							if (!guiAs.length && guiBs.length) {
								return 0;
							} else if (guiAs.length && !guiBs.length) {
								return 1;
							} else {
								return 2;
							}
						}
					} else {
						if (!guiAs.length && guiBs.length) {
							return 0;
						} else if (guiAs.length && !guiBs.length) {
							return 1;
						} else {
							return 2;
						}
					}
				}
			} else {
				return 0;
			}
		} else if (this.GetTongHuaShun(bCards).length != 0) {
			return 1;
		}

		////////////////////炸弹
		/*if(this.GetZhaDang(aCards).length != 0){
			if(this.CheckCardType(bCards) > 8)
				return 1;
			else if(this.GetZhaDang(bCards).length != 0){
				for(let i = 0; i < aCards.length; i++){
					let poker = aCards[i];
					let duizi = this.GetSameValue(aCards, poker);
					if(duizi.length >= 2){
						aValue = this.GetCardValue(duizi[0]);
						break;
					}
				}

				for(let i = 0; i < bCards.length; i++){
					let poker = bCards[i];
					let duizi = this.GetSameValue(bCards, poker);
					if(duizi.length >= 2){
						bValue = this.GetCardValue(duizi[0]);
						break;
					}
				}

				if(aValue > bValue)
					return 0;
				else if(aValue < bValue)
					return 1;
				else{
					if(!guiAs.length && guiBs.length){
						return 1;
					}
					else if(guiAs.length && !guiBs.length){
						return 0;
					}
					else{
						return 2;
					}
				}
			}
			else
				return 0;
		}
		else if(this.GetZhaDang(bCards).length != 0) {
			return 1;
		}*/

		////////////////////葫芦
		/*if(this.GetHulu(aCards).length != 0){
			if(this.CheckCardType(bCards) > 7) {
				return 1;
			} else if(this.CheckCardType(bCards) == 7){
				if(guiAs.length && guiBs.length){
					let aList = [];
					let bList = [];
					for(let i = 0; i < aCards.length; i++){
						if(guiAs.indexOf(aCards[i]) != -1) continue;
						let value = this.GetCardValue(aCards[i]);
						aList.push(value);
					}
					for(let i = 0; i < bCards.length; i++){
						if(guiBs.indexOf(bCards[i]) != -1) continue;
						let value = this.GetCardValue(bCards[i]);
						bList.push(value);
					}

					aList.sort(this.sortCardValue);
					bList.sort(this.sortCardValue);

					aValue = this.GetCardValue(aList[0]);
					bValue = this.GetCardValue(bList[0]);

					if(aValue > bValue)
						return 0;
					else if(aValue < bValue)
						return 1;
					else{
						return 2;
					}
				}
				else if(!guiAs.length && guiBs.length){
					let bList = [];
					let tempASantiao = this.GetSanTiaoEx(aCards,false);
					for(let i = 0; i < bCards.length; i++){
						if(guiBs.indexOf(bCards[i]) != -1) continue;
						let value = this.GetCardValue(bCards[i]);
						bList.push(value);
					}

					bList.sort(this.sortCardValue);

					aValue = this.GetCardValue(tempASantiao[0]);
					bValue = this.GetCardValue(bList[0]);
					if(aValue > bValue)
						return 0;
					else if(aValue < bValue)
						return 1;
					else{
						return 0;
					}
				}
				else if(guiAs.length && !guiBs.length){
					let aList = [];
					let tempBSantiao = this.GetSanTiaoEx(bCards,false);
					for(let i = 0; i < aCards.length; i++){
						if(guiAs.indexOf(aCards[i]) != -1) continue;
						let value = this.GetCardValue(aCards[i]);
						aList.push(value);
					}

					aList.sort(this.sortCardValue);

					aValue = this.GetCardValue(aList[0]);
					bValue = this.GetCardValue(tempBSantiao[0]);

					if(aValue > bValue)
						return 0;
					else if(aValue < bValue)
						return 1;
					else{
						return 1;
					}
				}
				else if(!guiAs.length && !guiBs.length){
					var tempASantiao = this.GetSanTiaoEx(aCards,false);
					var tempBSantiao = this.GetSanTiaoEx(bCards,false);
					aValue = this.GetCardValue(tempASantiao[0]);
					bValue = this.GetCardValue(tempBSantiao[0]);
					if(aValue > bValue)
						return 0;
					else if(aValue < bValue)
						return 1;
					else
						return 2;
				}
			} else {
				return 0;
			}

		} else if(this.GetHulu(bCards).length != 0) {
			return 1;
		}*/
		////////////////////两对同花
		/*if(this.GetLiangDuiTongHua(aCards).length != 0){
			if(this.CheckCardType(bCards) > 6)
				return 1;
			else if(this.CheckCardType(bCards) == 6){
				let aList = [];
				let bList = [];

				for(let i = 0; i < aCards.length; i++){
					if(guiAs.indexOf(aCards[i]) != -1) continue;
					let value = this.GetCardValue(aCards[i]);
					aList.push(value);
				}

				for(let i = 0; i < bCards.length; i++){
					if(guiBs.indexOf(bCards[i]) != -1) continue;
					let value = this.GetCardValue(bCards[i]);
					bList.push(value);
				}

				aList.sort(this.sortCardValue);
				bList.sort(this.sortCardValue);

				let tempAList = [];
				let tempBList = [];

				for(let i in aList){
					if(tempAList.indexOf(aList[i]) == -1)
						tempAList.push(aList[i]);
				}

				for(let i in bList){
					if(tempBList.indexOf(bList[i]) == -1)
						tempBList.push(bList[i]);
				}

				aValue = this.GetCardValue(tempAList[0]);
				bValue = this.GetCardValue(tempBList[0]);

				if(aValue > bValue)
					return 0;
				else if(aValue < bValue)
					return 1;
				else{
					aValue = this.GetCardValue(tempAList[1]);
					bValue = this.GetCardValue(tempAList[1]);

					if(aValue > bValue)
						return 0;
					else if(aValue < bValue)
						return 1;
					else{
						aValue = this.GetCardValue(tempAList[2]);
						bValue = this.GetCardValue(tempAList[2]);
						if(aValue > bValue)
							return 0;
						else if(aValue < bValue)
							return 1;
						else{
							if(!guiAs.length && guiBs.length)
								return 0;
							else if(guiAs.length && !guiBs.length)
								return 1;
							else
								return 2;
						}
					}
				}
			}
			else{
				return 0;
			}
		}
		else if(this.GetLiangDuiTongHua(bCards).length != 0)
			return 1;*/

		////////////////////一对同花

		if (this.GetYiDuiTongHua(aCards).length != 0) {
			if (this.CheckCardType(bCards) > 5) {
				return 1;
			} else if (this.CheckCardType(bCards) == 5) {
				let tempA = -1;
				let tempB = -1;
				let duiAList = this.GetDuiZi(aCards, false);
				let duiBList = this.GetDuiZi(bCards, false);

				let tempAList = [];
				let tempBList = [];

				if (duiAList.length) {
					tempA = this.GetCardValue(duiAList[0][0]);
				}
				if (duiBList.length) {
					tempB = this.GetCardValue(duiBList[0][0]);
				}

				if (tempA == -1) {
					for (let i = 0; i < aCards.length; i++) {
						if (guiAs.indexOf(aCards[i]) != -1) continue;
						let value = this.GetCardValue(aCards[i]);
						tempAList.push(value);
					}
					tempAList.sort(this.sortCardValue);
					tempA = tempAList[0];
				}

				if (tempB == -1) {
					for (let i = 0; i < bCards.length; i++) {
						if (guiBs.indexOf(bCards[i]) != -1) continue;
						let value = this.GetCardValue(bCards[i]);
						tempBList.push(value);
					}
					tempBList.sort(this.sortCardValue);
					tempB = tempBList[0];
				}

				if (tempA > tempB) {
					return 0;
				} else if (tempA < tempB) {

				} else {
					//AB數組衹有三個元素
					let duiA = [];
					let duiB = [];
					for (let i = 0; i < aCards.length; i++) {
						if (guiAs.indexOf(aCards[i]) != -1) continue;
						let value = this.GetCardValue(aCards[i]);
						if (tempA == value) continue;
						duiA.push(value);
					}
					duiA.sort(this.sortCardValue);
					for (let i = 0; i < bCards.length; i++) {
						if (guiAs.indexOf(bCards[i]) != -1) continue;
						let value = this.GetCardValue(bCards[i]);
						if (tempB == value) continue;
						duiB.push(value);
					}
					duiB.sort(this.sortCardValue);

					for (let i = 0; i < duiA.length; i++) {
						aValue = duiA[i];
						bValue = duiB[i];
						if (aValue > bValue) {
							return 0;
						} else if (aValue < bValue) {
							return 1;
						} else {
							if (i == duiA.length - 1) {
								if (!guiAs.length && guiBs.length) {

								} else if (guiAs.length && !guiBs.length) {
									return 1;
								} else {
									return 2;
								}
							}
							else {
								continue;
							}
						}
					}
				}
			} else {
				return 0;
			}
		} else if (this.GetYiDuiTongHua(bCards).length != 0) {
			return 1;
		}

		////////////////////同花
		if (this.GetTonghuaEx(aCards).length != 0) {
			if (this.CheckCardType(bCards) > 4) {
				return 1;
			} else if (this.CheckCardType(bCards) == 4) {
				aValue = tempSortAcards[tempSortAcards.length - 1];
				bValue = tempSortBcards[tempSortBcards.length - 1];
				if (aValue > bValue) {
					return 0;
				} else if (aValue < bValue) {
					return 1;
				} else {
					let forLength = 0;
					if (tempSortAcards.length == tempSortBcards.length) {
						forLength = tempSortAcards.length;
					} else {
						forLength = tempSortAcards.length > tempSortBcards.length ? tempSortBcards.length : tempSortAcards.length;
					}
					for (let j = forLength; j > 0; j--) {
						aValue = tempSortAcards[j];
						bValue = tempSortBcards[j];
						if (aValue > bValue) {
							return 0;
						} else if (aValue < bValue) {
							return 1;
						} else {
							if (0 == j) {
								return 2;
							}
						}
					}
				}
			} else {
				return 0;
			}
		} else if (this.GetTonghuaEx(bCards).length != 0) {
			return 1;
		}

		////////////////////顺子
		if (this.GetShunziEx(aCards).length != 0) {
			if (this.CheckCardType(bCards) > 3) {
				return 1;
			} else if (this.CheckCardType(bCards) == 3) {
				let aList = [];
				let bList = [];

				for (let i = 0; i < aCards.length; i++) {
					if (guiAs.indexOf(aCards[i]) != -1) continue;
					let value = this.GetCardValue(aCards[i]);
					aList.push(value);
				}

				for (let i = 0; i < bCards.length; i++) {
					if (guiBs.indexOf(bCards[i]) != -1) continue;
					let value = this.GetCardValue(bCards[i]);
					bList.push(value);
				}

				aList.sort(this.sortFun);
				bList.sort(this.sortFun);

				tempSortAcards = this.GetCompleteShun(aList);
				tempSortBcards = this.GetCompleteShun(bList);

				aValue = tempSortAcards[tempSortAcards.length - 1];
				bValue = tempSortBcards[tempSortBcards.length - 1];
				if (aValue > bValue) {
					return 0;
				} else if (aValue < bValue) {
					return 1;
				} else {//要判断下A
					if (14 == aValue && 14 == bValue) {
						aValue = tempSortAcards[tempSortAcards.length - 2];
						bValue = tempSortBcards[tempSortBcards.length - 2];
						if (aValue > bValue) {
							return 0;
						} else if (aValue < bValue) {
							return 1;
						} else {
							if (!guiAs.length && guiBs.length) {
								return 0;
							} else if (guiAs.length && !guiBs.length) {
								return 1;
							} else {
								return 2;
							}
						}
					} else {
						if (!guiAs.length && guiBs.length) {
							return 0;
						} else if (guiAs.length && !guiBs.length) {
							return 1;
						} else {
							return 2;
						}
					}
				}
			} else {
				return 0;
			}
		} else if (this.GetShunziEx(bCards).length != 0) {
			return 1;
		}

		////////////////////三条
		if (this.GetSanTiaoEx(aCards).length != 0) {
			if (this.CheckCardType(bCards) > 2) {
				return 1;
			} else if (this.CheckCardType(bCards) == 2) {
				if (guiAs.length && guiBs.length) {
					var tempADuizi = this.GetDuiZi(aCards, false);
					var tempBDuizi = this.GetDuiZi(bCards, false);
					aValue = this.GetCardValue(tempADuizi[0][0]);
					bValue = this.GetCardValue(tempBDuizi[0][0]);
					if (aValue > bValue) {
						return 0;
					} else if (aValue < bValue) {

					} else {
						return 2;
					}
				} else if (!guiAs.length && guiBs.length) {
					let santiaoA = this.GetSanTiaoEx(aCards, false);
					aValue = this.GetCardValue(santiaoA[0]);

					if (guiBs.length == 1) {
						let duiizs = this.GetDuiZi(bCards, false);
						bValue = this.GetCardValue(duiizs[0][0]);
					} else if (guiBs.length == 2) {
						let bList = [];
						for (let i = 0; i < bCards.length; i++) {
							if (guiBs.indexOf(bCards[i]) != -1) continue;
							bList.push(bCards[i]);
						}
						bList.sort(this.sortCardValue);
						bValue = this.GetCardValue(bList[0]);
					}

					if (aValue > bValue) {
						return 0;
					} else if (aValue < bValue) {
						return 1;
					} else {
						return 0;
					}
				} else if (guiAs.length && !guiBs.length) {
					let santiaoB = this.GetSanTiaoEx(bCards, false);
					bValue = this.GetCardValue(santiaoB[0]);

					if (guiAs.length == 1) {
						let duiizs = this.GetDuiZi(aCards, false);
						aValue = this.GetCardValue(duiizs[0][0]);
					} else if (guiAs.length == 2) {
						let aList = [];
						for (let i = 0; i < aCards.length; i++) {
							if (guiAs.indexOf(aCards[i]) != -1) continue;
							aList.push(aCards[i]);
						}
						aList.sort(this.sortCardValue);
						aValue = this.GetCardValue(aList[0]);
					}

					if (aValue > bValue) {
						return 0;
					} else if (aValue < bValue) {
						return 1;
					} else {
						return 1;
					}
				} else if (!guiAs.length && !guiBs.length) {
					let santiaoA = this.GetSanTiaoEx(aCards, false);
					let santiaoB = this.GetSanTiaoEx(bCards, false);
					aValue = this.GetCardValue(santiaoA[0]);
					bValue = this.GetCardValue(santiaoB[0]);
					if (aCards.length < 3 && bCards.length == 5) {
						return 1;
					} else if (aValue > bValue) {
						return 0;
					} else {
						return 2;
					}
				}
			} else {
				return 0;
			}
		} else if (this.GetSanTiaoEx(bCards).length != 0)
			return 1;

		//根据鬼牌挑选最大的对子
		if (guiAs.length && !duiziAs.length) {
			this.CheckDuiziByGui(aCards, guiAs, duiziAs);
		} else if (guiAs.length && duiziAs.length == 1) {
			this.CheckDuiziByGui(aCards, guiAs, duiziAs);
		}

		if (guiBs.length && !duiziBs.length) {
			this.CheckDuiziByGui(bCards, guiBs, duiziBs);
		} else if (guiBs.length && duiziBs.length == 1) {
			this.CheckDuiziByGui(bCards, guiBs, duiziBs);
		}

		////////////////////两对
		/*if(duiziAs.length == 2){
			if(this.CheckCardType(bCards) > 1)
				return 1;
			else if(duiziBs.length == 2){
				let tempSortDuiZiA = [];
				let tempSortDuiZiB = [];
				tempSortDuiZiA[0] = this.GetCardValue(duiziAs[0][0]);
				tempSortDuiZiA[1] = this.GetCardValue(duiziAs[1][0]);
				tempSortDuiZiB[0] = this.GetCardValue(duiziBs[0][0]);
				tempSortDuiZiB[1] = this.GetCardValue(duiziBs[1][0]);
				tempSortDuiZiA.sort(this.sortFun);
				tempSortDuiZiB.sort(this.sortFun);
				aValue = tempSortDuiZiA[0];
				bValue = tempSortDuiZiB[0];
				let aValueEx = tempSortDuiZiA[1];
				let bValueEx = tempSortDuiZiB[1];
				if(aValue == bValue && aValueEx == bValueEx){//相同的两对
					var remainCardA = -1;
					var remainCardB = -1;
					for(var i=0;i<5;i++){
						if(this.GetCardValue(aCards[i]) != tempSortDuiZiA[0] && this.GetCardValue(aCards[i]) != tempSortDuiZiA[1])
							remainCardA = this.GetCardValue(aCards[i]);
						if(this.GetCardValue(bCards[i]) != tempSortDuiZiB[0] && this.GetCardValue(bCards[i]) != tempSortDuiZiB[1])
							remainCardB = this.GetCardValue(bCards[i]);
					}
					if (remainCardA > remainCardB)
						return 0;
					else
						return 1;
				}
				if(aValueEx > bValueEx)//取最大
					return 0;
				else if(aValueEx == bValueEx){
					if(aValue > bValue)
						return 0;
					else
						return 1;
				}
				else
					return 1;
			}
			else
				return 0;
		} else if(duiziBs.length == 2) {
			return 1;
		}*/

		////////////////////一对
		if (duiziAs.length == 1) {
			if (this.CheckCardType(bCards) > 0) {
				return 1;
			} else if (duiziBs.length == 1) {
				let tempA = -1;
				let tempB = -1;
				let duiAList = this.GetDuiZi(aCards, false);
				let duiBList = this.GetDuiZi(bCards, false);

				let tempAList = [];
				let tempBList = [];

				if (duiAList.length) {
					tempA = this.GetCardValue(duiAList[0][0]);
				}
				if (duiBList.length) {
					tempB = this.GetCardValue(duiBList[0][0]);
				}

				if (tempA == -1) {
					for (let i = 0; i < aCards.length; i++) {
						if (guiAs.indexOf(aCards[i]) != -1) continue;
						let value = this.GetCardValue(aCards[i]);
						tempAList.push(value);
					}
					tempAList.sort(this.sortCardValue);
					tempA = tempAList[0];
				}

				if (tempB == -1) {
					for (let i = 0; i < bCards.length; i++) {
						if (guiBs.indexOf(bCards[i]) != -1) continue;
						let value = this.GetCardValue(bCards[i]);
						tempBList.push(value);
					}
					tempBList.sort(this.sortCardValue);
					tempB = tempBList[0];
				}

				if (tempA > tempB) {
					return 0;
				} else if (tempA < tempB) {
					return 1;
				} else {
					//AB數組衹有三個元素或者一個元素
					let duiA = [];
					let duiB = [];
					for (let i = 0; i < aCards.length; i++) {
						if (guiAs.indexOf(aCards[i]) != -1) continue;
						let value = this.GetCardValue(aCards[i]);
						if (tempA == value) continue;
						duiA.push(value);
					}
					duiA.sort(this.sortCardValue);
					for (let i = 0; i < bCards.length; i++) {
						if (guiAs.indexOf(bCards[i]) != -1) continue;
						let value = this.GetCardValue(bCards[i]);
						if (tempB == value) continue;
						duiB.push(value);
					}
					duiB.sort(this.sortCardValue);

					if (duiA.length == 1) {
						aValue = duiA[0];
						bValue = duiB[0];
						if (aValue > bValue) {
							return 0;
						} else if (aValue < bValue) {
							return 1;
						} else {
							if (!guiAs.length && guiBs.length) {
								return 0;
							} else if (guiAs.length && !guiBs.length) {
								return 1;
							} else {
								return 2;
							}
						}
					} else {
						for (let i = 0; i < duiA.length; i++) {
							aValue = duiA[i];
							bValue = duiB[i];
							if (aValue > bValue) {
								return 0;
							} else if (aValue < bValue) {
								return 1;
							} else {
								if (i == duiA.length - 1) {
									if (!guiAs.length && guiBs.length) {
										return 0;
									} else if (guiAs.length && !guiBs.length) {
										return 1;
									} else {
										return 2;
									}
								} else {
									continue;
								}
							}
						}
					}
				}
			} else {
				return 0;
			}
		} else if (duiziBs.length == 1) {
			return 1;
		}

		////////////////////垃圾牌
		if (this.CheckCardType(bCards) >= 0) {
			console.log("垃圾牌 ：副牌大");
			return 1;
		} else {
			console.log("同样垃圾牌");
			aValue = tempSortAcards[tempSortAcards.length - 1];
			bValue = tempSortBcards[tempSortBcards.length - 1];
			if (aValue > bValue) {
				return 0;
			} else if (aValue < bValue) {
				return 1;
			} else {
				//可能有3张和5张的比
				let lastIndexA = tempSortAcards.length - 1;
				let lastIndexB = tempSortBcards.length - 1;

				for (let j = 5; j > 0; j--) {
					if (lastIndexA < 0 || lastIndexB < 0)//如果3张或5张比下来都没结果就是相同大
						return 2;
					aValue = tempSortAcards[lastIndexA];
					bValue = tempSortBcards[lastIndexB];
					if (aValue > bValue) {
						return 0;
					} else if (aValue < bValue) {
						return 1;
					} else {
						if (0 == j) {
							return 2;
						}
					}
					lastIndexA--;
					lastIndexB--;
				}
			}
		}
	},

	sortCardValue: function (a, b) {
		return b - a;
	},

	GetCompleteShun: function (list) {
		let OverFive = false;
		for (let i = 0; i < list.length; i++) {
			if (list[i] == 14) continue;
			if (list[i] > 5) {
				OverFive = true;
			}
		}

		if (OverFive) {
			for (let i = 0; i < list.length - 1; i++) {
				let len = list[i + 1] - list[i];
				for (let j = 1; j < len; j++) {
					list.push(list[i] + j);
				}
			}

			list.sort(this.sortFun);//从小到大排序
			let value = list[list.length - 1];
			let addValue = 1;
			let len = list.length;

			for (let i = 1; i <= 5 - len; i++) {
				if (value == 14) {
					value = list[0];
					addValue = -1;
				}
				value = value + addValue;
				list.push(value);
			}
			list.sort(this.sortFun);//从小到大排序从小到大排序
		} else {
			list = [2, 3, 4, 5, 14];
		}
		return list;
	},

	GetGuiPai: function (pokers) {
		let guipai = [];
		/*for(let i = 0; i < pokers.length; i++){
			let poker = pokers[i];
			let newPoker = this.PokerCard.SubCardValue(poker);
			let ten = parseInt(newPoker,16);
			if(ten >= 65){
				guipai.push(poker);
			}
		}*/
		return guipai;
	},

	SanTongHua: function (pokers) {
		let gui = this.GetGuiPai(pokers);
		let tonghuas = [];
		for (let i = 0; i < pokers.length; i++) {
			let poker = pokers[i];
			if (gui.indexOf(poker) != -1) continue;
			let tonghua = this.GetSameColor(pokers, poker);
			let bInList = this.CheckPokerInList(tonghuas, poker);
			if (tonghua.length >= 5 && !bInList) {
				tonghuas[tonghuas.length] = tonghua;
			} else if (tonghua.length < 5 && !bInList) {
				tonghuas[tonghuas.length] = tonghua;
			}
		}

		tonghuas.sort(function (a, b) {
			return a.length - b.length;
		});

		let JoinGuiPia = function (tonghua, gui, len) {
			if (tonghua.length == len) return;
			for (let i = 0; i < gui.length; i++) {
				if (gui[i] == 'undefined') continue;
				tonghua.push(gui[i]);
				gui[i] = 'undefined';
				if (tonghua.length == len) {
					break;
				}
			}
		};

		if (gui.length) {
			JoinGuiPia(tonghuas[0], gui, 3);
			JoinGuiPia(tonghuas[1], gui, 5);
			JoinGuiPia(tonghuas[2], gui, 5);
		}

		return tonghuas;
	},

	PokerCombination: function (index, list, lists) {
		var rs = [];
		if (index == 2) {
			for (var j = 0; j < list.length; j++) {
				for (var k = j; k < list.length; k++) {
					var map = {};
					map.key = list[j];
					if (k != j) {
						map.value = list[k];
						rs.push(map);
					}
				}
			}
		} else if (index == 3) {
			for (var j = 0; j < list.length; j++) {
				for (var k = j; k < list.length; k++) {
					var map = {};
					map.key = list[j];
					if (k != j && list[k + 1]) {
						map.value = list[k];
						map.x = list[k + 1];
						rs.push(map);
					}
				}
			}
		} else if (index == 4) {
			for (var j = 0; j < list.length; j++) {
				for (var k = j; k < list.length; k++) {
					var map = {};
					map.key = list[j];
					if (k != j && list[k + 2]) {
						map.value = list[k];
						map.x = list[k + 1];
						map.x1 = list[k + 2];
						rs.push(map);
					}
				}
			}
		} else if (index == 5) {
			for (var j = 0; j < list.length; j++) {
				for (var k = j; k < list.length; k++) {
					var map = {};
					map.key = list[j];
					if (k != j && list[k + 3]) {
						map.value = list[k];
						map.x = list[k + 1];
						map.x1 = list[k + 2];
						map.x2 = list[k + 3];
						rs.push(map);
					}
				}
			}
		}

		//将组合好的牌型放入lists
		for (let idx = 0; idx < rs.length; idx++) {
			let data = rs[idx];
			let temp = [];
			for (let i in data) {
				temp.push(data[i]);
			}

			lists[lists.length] = temp;
		}
	},

	GetCombinations:function(arr, n) {
		const result = [];
	
		function helper(start, current) {
			if (current.length === n) {
				result.push([...current]);
				return;
			}
	
			for (let i = start; i < arr.length; i++) {
				current.push(arr[i]);
				helper(i + 1, current);
				current.pop();
			}
		}
	
		helper(0, []);
		return result;
	},

	/**
 * 两个数组的差集
 * @param {*} arr1 手牌 
 * @param {*} arr2 癞子排
 * @returns  手牌里除去癞子牌后剩余的牌
 */
	arrayDifference: function (arr1, arr2) {
		return arr1.filter(item => !arr2.includes(item));
	},

	/**
	 * 两个数组的交集
	 * @param {*} arr1 手牌 
	 * @param {*} arr2 癞子排
	 * @returns 手牌的癞子牌数组
	 */
	arrayIntersection: function (arr1, arr2) {
		return arr1.filter(item => arr2.includes(item));
	},


	//判断是否是三条    
	JudejeIsSanTiao: function (cards) {
		let obj = {
			isMatch: false,
			list: cards.concat(),
			type: "",
		}
		if (cards.length < 3) {
			return obj;
		}

		//除去癞子的剩余手牌
		let hands = this.arrayDifference(cards, this.LaiZis);
		let laiZiList = this.arrayIntersection(cards, this.LaiZis);

		if (laiZiList.length === 0) { //没有癞子的三条
			const v0 = this.GetCardValue(cards[0]);
			const v1 = this.GetCardValue(cards[1]);
			const v2 = this.GetCardValue(cards[2]);
			if (v1 === v0 && v2 === v0) {
				obj.isMatch = true;
				obj.type = "santiao";
				return obj;
			} else {
				return obj;
			}
		} else {
			const v0 = this.GetCardValue(hands[0]);
			const v1 = this.GetCardValue(hands[1]);
			if (laiZiList.length === 1 && v0 === v1) {
				obj.isMatch = true;
				obj.list = hands.concat(laiZiList);
				obj.type = "santiao";
				return obj;
			} else if (laiZiList.length === 2) {
				obj.isMatch = true;
				obj.list = hands.concat(laiZiList);
				obj.type = "santiao";
				return obj;
			} else {
				return obj;
			}
		}
	},

	//判断是否是同花顺
	JudejeIsTongHuaShun: function (cards) {
		let obj = {
			isMatch: false,
			list: cards.concat(),
			type: "",
		}
		if (cards.length < 3) {
			return obj;
		}

		let objTH = this.JudejeIsTongHua(cards);
		let objSZ = this.JudejeIsShunZi(cards);
		if (objTH.isMatch && objSZ.isMatch) { //同花和顺子都符合
			obj.isMatch = true;
			obj.list = objSZ.list.concat();
			obj.type = 'ths';
			return obj;
		}
		return obj;
	},

	// 判断是否是同花
	JudejeIsTongHua: function (cards) {
		let obj = {
			isMatch: false,
			list: cards.concat(),
			type: "",
		}
		if (cards.length < 3) {
			return obj;
		}

		//除去癞子的剩余手牌
		let hands = this.arrayDifference(cards, this.LaiZis);
		let laiZiList = this.arrayIntersection(cards, this.LaiZis);

		if (laiZiList.length === 0) { //正常的同花（不带癞子）
			//花色升序
			cards.sort((a, b) => {
				return this.GetCardColor(a) - this.GetCardColor(b);
			})
			cards.list = cards.concat();

			const c0 = this.GetCardColor(cards[0]);
			const c1 = this.GetCardColor(cards[1]);
			const c2 = this.GetCardColor(cards[2]);

			if (c0 === c1 && c1 === c2) {
				obj.isMatch = true;
				obj.list = cards.concat();
				obj.type = "th";
				return obj;
			} else {
				return obj;
			}
		} else {
			if (laiZiList.length === 1) {
				const c0 = this.GetCardColor(hands[0]);
				const c1 = this.GetCardColor(hands[1]);
				if (c0 === c1) {
					obj.isMatch = true;
					obj.list = hands.concat(laiZiList);
					obj.type = "th";
					return obj;
				} else {
					return obj;
				}
			}
		}
	},

	//判断是否是顺子
	JudejeIsShunZi: function (cards) {
		let obj = {
			isMatch: false,
			list: cards.concat(),
			type: ""
		}
		if (cards.length < 3) {
			return obj;
		}

		//除去癞子的剩余手牌
		let hands = this.arrayDifference(cards, this.LaiZis);
		let laiZiList = this.arrayIntersection(cards, this.LaiZis);

		//正常的顺子
		if (laiZiList.length === 0) { //正常的顺子（不带癞子）
			//牌值升序
			cards.sort((a, b) => {
				return this.GetCardValue(a) - this.GetCardValue(b);
			})
			const v0 = this.GetCardValue(cards[0]);
			const v1 = this.GetCardValue(cards[1]);
			const v2 = this.GetCardValue(cards[2]);
			if (v1 === v0 + 1 && v1 + 1 === v2) {  //正常的顺子，如 2,3,4
				obj.isMatch = true;
				obj.list = cards.concat();
				obj.type = "sz";
				return obj;
			}

			//特殊顺子 A 2 3
			if (v0 === 2 && v1 === 3 && v2 === 14) {
				let temp = cards[2];
				cards[2] = cards[1];
				cards[1] = cards[0];
				cards[0] = temp;

				obj.isMatch = true;
				obj.list = cards.concat();
				obj.type = "sz";
				return obj;
			}

		} else {
			if (laiZiList.length === 1) { //一个癞子
				hands.sort((a, b) => {
					return this.GetCardValue(a) - this.GetCardValue(b);
				})
				const v0 = this.GetCardValue(hands[0]);
				const v1 = this.GetCardValue(hands[1]);
				if (v0 + 1 === v1 && v1 < 14) { // 如： [2,3, 癞子]牌型，癞子补在最后
					obj.isMatch = true;
					obj.list = hands.concat(laiZiList);
					obj.type = "sz";
					return obj;
				} else if (v0 + 1 === v1 && v1 === 14) { //碰到最大的 QKA牌型，癞子充当 Q
					obj.isMatch = true;
					obj.list = laiZiList.concat(hands);
					obj.type = "sz";
					return obj;
				} else if (v1 - v0 === 2) { //  2 3 4 牌型，癞子充当 3
					obj.isMatch = true;
					obj.list = [hands[0], laiZiList[0], hands[1]];
					obj.type = "sz";
					return obj;
				} else {
					return obj;
				}
			} else {
				return obj;
			}
		}
		return obj;
	},

	//判断是否是对子
	JudejeIsDuiZi: function (cards) {
		let obj = {
			isMatch: false,
			list: cards.concat(),
			type: ''
		}
		if (cards.length < 2) {
			return obj;
		}

		//除去癞子的剩余手牌
		let hands = this.arrayDifference(cards, this.LaiZis);
		let laiZiList = this.arrayIntersection(cards, this.LaiZis);
		if (laiZiList.length === 0) { //没有癞子
			hands.sort((a, b) => {
				return this.GetCardValue(a) - this.GetCardValue(b);
			})
			const v0 = this.GetCardValue(hands[0]);
			const v1 = this.GetCardValue(hands[1]);
			if (v0 === v1) {
				obj.isMatch = true;
				hands.sort((a, b) => {
					return this.GetCardColor(a) - this.GetCardColor(b);
				})
				obj.list = hands.concat();
				obj.type = "duizi";
				return obj;
			}
		} else {
			if (laiZiList.length === 1 && hands.length === 1) {
				obj.isMatch = true;
				obj.list = hands.concat(laiZiList);
				obj.type = "duizi";
				return obj;
			}
		}
		return obj;
	},

	//判断是否是单牌
	JudejeIsDanCard: function (cards) {
		let obj = {
			isMatch: false,
			list: cards.concat(),
			type: ''
		}
		if (cards.length < 2) {
			return obj;
		}

		let hands = this.arrayDifference(cards, this.LaiZis);
		let laiZiList = this.arrayIntersection(cards, this.LaiZis);
		if (hands.length === 0) {  //全是癞子排
			return null;
		}

		cards.sort((a, b) => {
			return this.GetCardValue(a) - this.GetCardValue(b);
		})
		obj.isMatch = true;
		obj.type = 'danpai';
		obj.list = cards.concat();
		return obj;
	},

	//判断牌型：优先级     三条 > 同花顺 > 同花 > 顺子 > 对子 > 单张
	JudejeCardType: function (cards) {
		let resList = [];
		resList = cards.concat();

		//判断是否是三条
		let obj = this.JudejeIsSanTiao(resList);
		if (obj.isMatch) { //三条
			console.log('=========isMatch 三条============', resList)
			return obj;
		}

		//判断是否是同花顺
		obj = this.JudejeIsTongHuaShun(resList);
		if (obj.isMatch) {
			console.log('=========isMatch 同花顺============', resList);
			return obj;
		}

		//判断是否是同花
		obj = this.JudejeIsTongHua(resList)
		if (obj.isMatch) {
			console.log('=========isMatch 同花============', resList)
			return obj;
		}

		//判断是否是顺子
		obj = this.JudejeIsShunZi(resList);
		if (obj.isMatch) {
			console.log('=========isMatch 顺子============', resList);
			return obj;
		}

		//判断是否是对子
		obj = this.JudejeIsDuiZi(resList);
		if (obj.isMatch) {
			console.log('=========isMatch 对子============', resList);
			return obj;
		}

		//单张牌型
		obj = this.JudejeIsDanCard(resList);
		if (!obj) {
			console.log('=========isMatch 癞子数组============', resList);
			return null
		}
		if (obj.isMatch) {
			console.log('=========isMatch 单牌============', resList);
			return obj;
		}
		console.log('=========无类型============');
		return null
	},

	//对子
	GetDuiZi: function (pokers) {
		const cardCounts = {};
		const combinations = [];
	
		let cardList = pokers.concat();
		this.SortCardByMin(cardList);
	
		let laiziList = this.RemoveLiZiFromPokers(cardList);
		for (const card of cardList) {
			const point = this.GetCardValue(card); // 牌值
			const suit = this.GetCardColor(card); // 花色
			const key = `${point}`;
			if(!cardCounts[key]){
			  cardCounts[key] = {
				  value: point, //牌值
				  color: [suit], //花色
				  logicV: [card], //逻辑值 
			  }
			}else{
			  cardCounts[key].color.push(suit);
			  cardCounts[key].logicV.push(card);
			}
		}
	
		let allCollection = [];
		for (const key in cardCounts) {
			let obj = cardCounts[key];
			let listTemp = obj.logicV.concat(laiziList);
			let list = this.GetCombinations(listTemp, 2)
			allCollection = allCollection.concat(list);
		}
	
		for (let i = 0; i < allCollection.length; i++) {
			const listCur = allCollection[i];
			let res = this.JudejeCardType(listCur);
			if(res){
				let type = res.type;
				if(type === 'duizi'){
					combinations.push(res.list);
				}
			}
		} 
		console.log('顺子 ===>>',combinations);
		return combinations;
	},

	//顺子
	GetShunzi: function (pokers) {
		const cardCounts = {};
		const combinations = [];
		this.SortCardByMin(pokers);
	
		let allCollection= []; //所有的合集数据
	   
		let list = this.GetCombinations(pokers, 3)
		allCollection = allCollection.concat(list);
		console.log('====所以有的组合数======',allCollection.length, allCollection);
	
		//step2： 找出合集的最忧牌型
		for (let i = 0; i < allCollection.length; i++) {
			const listCur = allCollection[i];
			let obj = this.JudejeCardType(listCur);
			if(obj && obj.isMatch){
				console.log('========GetShunzi==========',JSON.stringify(obj));
				let type = obj.type;
				if(type === 'sz' || type === 'ths'){
					combinations.push(obj.list);
				}
			}
		}
		console.log('顺子 ===>>',combinations);
		return combinations;
	},

	//同花
	GetTonghua: function (pokers) {
		this.SortCardByMin(pokers);

		const cardCounts = {};
		const combinations = []; //结果数组
		let copyList = pokers.concat();
		// 癞子排
		let laiziList = this.RemoveLiZiFromPokers(copyList);
		console.log('====laiziList======', laiziList);
		for (const card of copyList) {
			const point = this.GetCardValue(card); // 牌值
			const suit = this.GetCardColor(card); // 花色
			const key = `${suit}`;
			if (!cardCounts[key]) {
				cardCounts[key] = {
					value: [point], //牌值
					color: suit, //花色
					logicV: [card], //逻辑值 
				}
			} else {
				cardCounts[key].value.push(point);
				cardCounts[key].logicV.push(card);
			}
		}
		//step1： 找出带癞子的所有合集
		console.log('====cardCounts======', cardCounts);
		let allCollection= []; //所有的合集数据
		for (const key in cardCounts) {
			let obj = cardCounts[key];
			let listTemp = obj.logicV.concat(laiziList);
			let list = this.GetCombinations(listTemp, 3)
			allCollection = allCollection.concat(list);
		}
		console.log('====所以有的组合数======',allCollection.length, allCollection);
		//step2： 找出合集的最忧牌型
		for (let i = 0; i < allCollection.length; i++) {
			const listCur = allCollection[i];
			let res = this.JudejeCardType(listCur);
			if(res){
				combinations.push(res.list);
			}
		}
		console.log("三同花=====>", JSON.stringify(combinations));
		return combinations;
	},

	//同花顺
	GetTongHuaShun: function (pokers) {

		const cardCounts = {};
		const combinations = [];
		this.SortCardByMin(pokers);
	
		let allCollection= []; //所有的合集数据
	   
		let list = this.GetCombinations(pokers, 3)
		allCollection = allCollection.concat(list);
		console.log('====所以有的组合数======',allCollection.length, allCollection);
	
		//step2： 找出合集的最忧牌型
		for (let i = 0; i < allCollection.length; i++) {
			const listCur = allCollection[i];
			let obj = this.JudejeCardType(listCur);
			if(obj && obj.isMatch){
				let type = obj.type;
				console.log('========JudejeCardType1212==========',obj.type);
				if(type === 'ths'){
					combinations.push(obj.list);
				}
			}
		}
		console.log("同花顺=====>", JSON.stringify(combinations));
		return combinations;
	},

	//三条
	GetSanTiao: function (pokers) {
		this.SortCardByMin(pokers);
	
		let cardList = pokers.concat();
		let laiziList = this.RemoveLiZiFromPokers(cardList);
	
		const cardCounts = {};
		const combinations = [];
		for (const card of cardList) {
		  const point = this.GetCardValue(card); // 牌值
		  const suit = this.GetCardColor(card); // 花色
		  const key = `${point}`;
		  if(!cardCounts[key]){
			cardCounts[key] = {
				value: point, //牌值
				color: [suit], //花色
				logicV: [card], //逻辑值 
			}
		  }else{
			cardCounts[key].color.push(suit);
			cardCounts[key].logicV.push(card);
		  }
		}
	  
		console.log('====laiziList======',laiziList);
		//step1： 找出带癞子的所有合集
		console.log('====cardCounts======', cardCounts);
		let allCollection= []; //所有的合集数据
		for (const key in cardCounts) {
			let obj = cardCounts[key];
			let listTemp = obj.logicV.concat(laiziList);
			let list = this.GetCombinations(listTemp, 3)
			allCollection = allCollection.concat(list);
		}
		console.log('====所以有的组合数======',allCollection.length, allCollection);
		//step2： 找出合集的最忧牌型
		for (let i = 0; i < allCollection.length; i++) {
			const listCur = allCollection[i];
			let res = this.JudejeCardType(listCur);
			if(res){
				let type = res.type;
				if(type === 'santiao'){
					combinations.push(res.list);
				}
			}
		}
		console.log("三条======>", JSON.stringify(combinations));
		return combinations;
	},

	//等到牌数组里面所有的对子
	GetAllDuiZi:function(pokers){
		const pairs = [];
		for (let i = 0; i < pokers.length - 1; i++) {
			let card1 = this.GetCardValue(pokers[i]);
			let card2 = this.GetCardValue(pokers[i+1]);
		  if (card1 === card2) {
			pairs.push([pokers[i], pokers[i + 1]]);
			i++; // 跳过已经处理的元素
		  }
		}
		return pairs;
	},

	GetAddCard: function (cards) {
		let copyPokers = cards.concat();
		for (let a = 0; a < cards.length; a++) {
			let copyPoker = cards[a];
			if (this.GetCardValue(copyPoker) == 14) {
				copyPokers.push(copyPoker);
			}
		}
		return copyPokers;
	},
	GetLiangDui: function (pokers) {
		let guipai = this.GetGuiPai(pokers);
		let duizis = [];
		for (let i = 0; i < pokers.length; i++) {
			let poker = pokers[i];
			if (guipai.indexOf(poker) != -1) continue;
			let duizi = this.GetSameValue(pokers, poker);
			let bInList = this.CheckPokerInList(duizis, poker);
			if (duizi.length >= 2 && !bInList) {
				duizis[duizis.length] = duizi;
			}
			if (guipai.length > 1) {
				duizis[duizis.length] = guipai;
			}
		}

		if (duizis.length >= 2) {
			return pokers
		} else {
			return [];
		}
	},

	GetSanTiaoEx: function (pokers, needCheckHuLu = true) {
		let guipai = this.GetGuiPai(pokers);
		let santiaos = [];
		if (!guipai.length) {
			for (let i = 0; i < pokers.length; i++) {
				let poker = pokers[i];
				let santiao = this.GetSameValue(pokers, poker);
				if (3 == santiao.length) {
					santiaos = santiao;
					break;
				}
			}
		} else if (guipai.length == 1) {
			for (let i = 0; i < pokers.length; i++) {
				let poker = pokers[i];
				if (guipai.indexOf(poker) != -1) continue;
				let duizi = this.GetSameValue(pokers, poker);
				if (2 == duizi.length) {
					duizi.push(guipai[0]);
					santiaos = duizi;
					break;
				}
			}
		} else if (guipai.length > 1) {
			for (let i = 0; i < pokers.length; i++) {
				let poker = pokers[i];
				if (guipai.indexOf(poker) != -1) continue;
				santiaos.push(poker);
				for (let j = 0; j < guipai.length; j++) {
					santiaos.push(guipai[j]);
				}
				break;
			}
		}

		if (3 == santiaos.length) {
			if (!needCheckHuLu)
				return santiaos;
			//判断下是不是葫芦，不能直接调用gethulu因为里面调用了这个函数会死循环
			let remianList = [];
			for (let i = 0; i < pokers.length; i++) {
				if (santiaos.indexOf(pokers[i]) != -1) continue;
				remianList.push(pokers[i]);
			}
			if (remianList.length == 2) {
				let duizi = this.GetSameValue(remianList, remianList[0]);
				if (duizi.length == 2) {
					return [];//是葫芦
				} else {
					console.log("三条 ：" + santiaos);
					return santiaos;//三条
				}
			} else {
				console.log("三条 ：" + santiaos);
				return santiaos;//三条
			}
		} else {
			console.log("三条 ： 空");
			return [];
		}
	},

	isSave: function (shunzi, poker) {
		let guipai = this.GetGuiPai(poker);

		if (guipai.length) {
			return false;
		}

		if (!this.isContain(shunzi, poker)) {
			return false;
		}

		if (shunzi.length == 1) {
			return true;
		}

		if (this.GetCardValue(shunzi[0]) == 14 && this.GetCardValue(shunzi[1]) <= 5) {
			return true;
		}

		let firstValue = this.GetCardValue(shunzi[0]);
		let lastValue = this.GetCardValue(poker);

		let value = firstValue - lastValue;
		if (value > 4) {
			return false;
		}

		return true;
	},

	isContain: function (shunzi, poker) {
		let temp = this.GetCardValue(poker);
		for (let i = 0; i < shunzi.length; i++) {
			let cardValue = this.GetCardValue(shunzi[i]);
			if (temp == cardValue) return false;
		}
		return true;
	},

	// checkListByAllList:function(cards,allCards){
	//     if(5 != cards.length)return true;
	//     for(let i=0;i<allCards.length;i++){
	//         let curCards = allCards[i];
	//         if(curCards[0] == cards[0] && curCards[1] == cards[1] && curCards[2] == cards[2]
	//             && curCards[3] == cards[3] && curCards[4] == cards[4])
	//             return true;
	//     }
	//     return false;
	// },
	sortFunEx: function (a, b) {
		if (a.cardValue == b.cardValue) {
			return a.cardX16 - b.cardX16;
		} else {
			return a.cardValue - b.cardValue;
		}
	},

	//将癞子数据从原数据里面取出来，会改变原来数组
	GetLaiZi:function(pokers){
		let laiZiList = pokers.filter((a)=>{
			return a === this.Logic_xiaowang || a === this.Logic_dawang;
		})
		return laiZiList;
	},

	/**
	 * 将拷贝的数据里面的癞子去除
	 * @param {*} pokers 这个是拷贝的数据	 
	 */
	RemoveLiZiFromPokers:function(pokers){
		let laiz = this.LaiZis;
		let res = [];
		for (let i = 0; i < laiz.length; i++) {
			const lz = laiz[i];
			let idx = pokers.indexOf(lz);
			if(idx!== -1){
				let temp = pokers.splice(idx,1)
				res.push(temp[0]);
			}
		}
		return res;
	},

	//对牌型进行排序(按牌值降序)
	SortCardByMax: function (pokers) {
		if (pokers.length == 0) {
			return;
		}
		let self = this;
		pokers.sort(function (a, b) {
			return self.GetCardValue(b) - self.GetCardValue(a);
		});

		console.log('=======SortCardByMax==========',JSON.stringify(pokers))
	},

	//对牌型进行排序(按牌值升序)
	SortCardByMin: function (pokers) {
		console.log('==========SortCardByMin==========', pokers);
		if (pokers.length == 0) {
			return;
		}

		if (pokers.length === 3) {

			//癞子这里就不在排序
			let laiZis = this.GetLaiZi(pokers);
			console.log('=======SortCardByMin=========',laiZis.length)
			if(laiZis.length === 0){//处理癞子
				let self = this;
				pokers.sort(function (a, b) {
					return self.GetCardValue(a) - self.GetCardValue(b);
				});
				let v1 = this.GetCardValue(pokers[0]);
				let v2 = this.GetCardValue(pokers[1]);
				let v3 = this.GetCardValue(pokers[2]);
				if (v3 == 14) { //处理A 特殊的
					console.log('======SortCardByMin=======', v1, v2, v3);
					console.log('======SortCardByMin 交换前=======', pokers);
					if (v1 === 2 && v2 === 3) { //最小牌型 A23
						let temp = pokers[2];
						pokers[2] = pokers[1];
						pokers[1] = pokers[0];
						pokers[0] = temp;
						console.log('======SortCardByMin 交换后=======', pokers);
					}
				}
			}else{
				let v1 = this.GetCardValue(pokers[0]); // 14 
				let v2 = this.GetCardValue(pokers[1]);	// 2
				let v3 = this.GetCardValue(pokers[2]); // 1
				console.log('======SortCardByMin1=======', v1, v2, v3);
				if (v3 == 14) { //处理A 特殊的
					console.log('======SortCardByMin=======', v1, v2, v3);
					console.log('======SortCardByMin 交换前=======', pokers);
					if (v1 === 2 && v2 === 3) { //最小牌型 A23
						let temp = pokers[2];
						pokers[2] = pokers[1];
						pokers[1] = pokers[0];
						pokers[0] = temp;
						console.log('======SortCardByMin 交换后=======', pokers);
					}
				}
			}
		} else {
			let self = this;
			pokers.sort(function (a, b) {
				return self.GetCardValue(a) - self.GetCardValue(b);
			});
		}
	},


	SortCardByMin2:function(poker){

	},

	GetShunziEx: function (pokers) {//只判断3张
		if (pokers.length != 5) {
			return [];
		}
		let guipai = this.GetGuiPai(pokers);
		let shunzis = [];
		let needCheckList = [];
		let hasAce = false;
		let changeAce = false;
		for (let i = 0; i < pokers.length; i++) {
			let sameValue = this.GetSameValue(pokers, pokers[i]);
			if (sameValue.length >= 2) {
				return [];
			}
			if (guipai.indexOf(pokers[i]) != -1) {
				continue;
			}
			let data = {};
			data.cardValue = this.GetCardValue(pokers[i]);
			if (data.cardValue == 14) {
				hasAce = true;
			}
			data.cardX16 = pokers[i];
			needCheckList.push(data);
		}

		if (hasAce) {
			for (let i = 0; i < needCheckList.length; i++) {
				let curValue = needCheckList[i].cardValue;
				if (curValue <= 3) {
					changeAce = true;
					break;
				}
			}
		}

		needCheckList.sort(this.sortFunEx);//排序后A很定不会在前面
		if (changeAce) {
			for (let i = 0; i < needCheckList.length; i++) {
				let curValue = needCheckList[i].cardValue;
				if (curValue == 14) {
					needCheckList[i].cardValue = 1;
					break;
				}
			}
			needCheckList.sort(this.sortFunEx);
		}
		console.log("length == " + needCheckList.length);
		let value = needCheckList[needCheckList.length - 1].cardValue - needCheckList[0].cardValue;
		if (value <= 2) {
			return pokers;
		}
		else {
			return [];
		}
		return [];
	},

	isSameColor: function (tonghuas, tonghua) {
		let bRet = false;
		if (tonghuas.length) {
			for (let idx = 0; idx < tonghuas.length; idx++) {
				let first = this.GetCardColor(tonghuas[idx][0]);
				let second = this.GetCardColor(tonghua[0]);
				if (first == second) {
					bRet = true;
					break;
				}
			}
		}
		return bRet;
	},


	GetTonghuaEx: function (pokers) {
		if (pokers.length != 3) {
			return [];
		}
		let isSameColor = this.CheckSameColor(pokers);
		if (pokers.length == 3 && isSameColor) {
			console.log("同花 ： " + pokers);
			return pokers;
		} else {
			console.log("同花 ： 空");
			return [];
		}
	},

	GetYiDuiTongHua: function (pokers) {
		if (pokers.length != 5) return [];
		let guipai = this.GetGuiPai(pokers);
		let isSameColor = this.CheckSameColor(pokers);
		if (pokers.length == 5 && isSameColor) {
			let isYidui = false;
			for (let i = 0; i < pokers.length; i++) {
				let poker = pokers[i];
				if (guipai.indexOf(poker) != -1) continue;
				let duizi = this.GetSameValue(pokers, poker);
				if (duizi.length == 2) {
					isYidui = true;
					break;
				}
			}
			if (guipai.length == 1) {
				isYidui = true;
			}
			if (isYidui) {
				console.log("一对同花 ： " + pokers);
				return pokers;
			} else {
				return [];
			}
		} else {
			console.log("同花 ： 空");
			return [];
		}
	},

	GetLiangDuiTongHua: function (pokers) {
		if (pokers.length != 5) return [];
		let guipai = this.GetGuiPai(pokers);
		let isSameColor = this.CheckSameColor(pokers);
		let duizi = [];
		if (pokers.length == 5 && isSameColor) {
			if (!guipai.length) {
				let duizis = this.GetDuiZi(pokers);
				if (duizis.length == 2) {
					console.log("两对同花 ： " + pokers);
					return pokers;
				} else {
					return [];
				}
			} else if (guipai.length == 1) {
				for (let i = 0; i < pokers.length; i++) {
					let poker = pokers[i];
					if (guipai.indexOf(poker) != -1) continue;
					duizi = this.GetSameValue(pokers, poker);
					if (duizi.length == 2) {
						break;
					}
				}

				if (duizi.length == 2) {
					return pokers;
				} else {
					return [];
				}
			} else if (guipai.length > 1) {
				return pokers;
			}
		} else {
			console.log("同花 ： 空");
			return [];
		}
	},

	GetHulu: function (pokers) {
		let guipai = this.GetGuiPai(pokers);
		let santiaos = [];
		if (!guipai.length) {
			for (let i = 0; i < pokers.length; i++) {
				let poker = pokers[i];
				let santiao = this.GetSameValue(pokers, poker);
				if (3 == santiao.length) {
					santiaos = santiao;
					break;
				}
			}
		} else if (guipai.length == 1) {
			for (let i = 0; i < pokers.length; i++) {
				let poker = pokers[i];
				if (guipai.indexOf(poker) != -1) continue;
				let duizi = this.GetSameValue(pokers, poker);
				if (2 == duizi.length) {
					duizi.push(guipai[0]);
					santiaos = duizi;
					break;
				}
			}
		} else if (guipai.length > 1) {
			for (let i = 0; i < pokers.length; i++) {
				let poker = pokers[i];
				if (guipai.indexOf(poker) != -1) continue;
				santiaos.push(poker);
				for (let j = 0; j < guipai.length; j++) {
					santiaos.push(guipai[j]);
				}
				break;
			}
		}

		if (santiaos.length == 0)
			return [];

		let remianList = [];
		for (let i = 0; i < pokers.length; i++) {
			if (santiaos.indexOf(pokers[i]) != -1) continue;
			remianList.push(pokers[i]);
		}
		if (remianList.length == 2) {
			let duizi = this.GetSameValue(remianList, remianList[0]);
			if (duizi.length == 2) {
				console.log("葫芦 ： " + pokers);
				return pokers;
			} else {
				console.log("葫芦 ： 空");
				return [];
			}
		} else {
			console.log("葫芦 ： 空");
			return [];
		}
	},

	GetZhaDang: function (pokers, isGui = true) {
		let guipai = this.GetGuiPai(pokers);
		let zhadans = [];
		for (let i = 0; i < pokers.length; i++) {
			let poker = pokers[i];
			if (guipai.indexOf(poker) != -1) continue;
			let zhadan = this.GetSameValue(pokers, poker);
			let bInList = this.CheckPokerInList(zhadans, poker);

			if (zhadan.length > 3 && !bInList) {
				this.PokerCombination(4, zhadan, zhadans);
			}
		}

		if (isGui) {
			if (guipai.length >= 4) {
				this.PokerCombination(4, guipai, zhadans);
			}

			if (guipai.length == 1) {
				let santiaos = this.GetSanTiao(pokers, false);
				for (let i = 0; i < santiaos.length; i++) {
					let santiao = santiaos[i];

					if (this.isHaveGui(guipai, santiao)) continue;

					santiao.push(guipai[0]);
					zhadans[zhadans.length] = santiao;
				}
			} else if (guipai.length > 1) {
				let duizis = this.GetDuiZi(pokers, false);
				for (let i = 0; i < duizis.length; i++) {
					let duizi = duizis[i]

					if (this.isHaveGui(guipai, duizi)) continue;

					for (let j = 0; j < guipai.length; j++) {
						duizi.push(guipai[j]);
					}
					zhadans[zhadans.length] = duizi;
				}
			}
		}


		console.log('炸弹 :', zhadans);
		return zhadans;
	},

	GetTongHuaShunEx: function (pokers) {
		let tonghuashuns = [];
		let shunzis = this.GetShunzi(pokers, true);
		for (let idx = 0; idx < shunzis.length; idx++) {
			let shunzi = shunzis[idx];
			let bSameColor = this.CheckSameColor(shunzi);
			if (bSameColor) {
				tonghuashuns[tonghuashuns.length] = shunzi;
			}
		}
		return tonghuashuns;
	},

	GetWuTong: function (pokers) {
		/*let guipai = this.GetGuiPai(pokers);
		let wutongs = [];
		for (let i = 0; i < pokers.length; i++) {
			let poker = pokers[i];
			if(guipai.indexOf(poker) != -1) continue;
			let wutong = this.GetSameValue(pokers, poker);
			let bInList = this.CheckPokerInList(wutongs, poker);
			if (wutong.length == 5 && !bInList){
				this.PokerCombination(5, wutong, wutongs);
			}
			else if(wutong.length > 5 && !bInList){
				this.PokerCombination(5, wutong, wutongs);
			}
		}

		if(guipai.length >= 5){
			this.PokerCombination(5, guipai, wutongs);
		}

		if(guipai.length == 1){
			let zhadans = this.GetZhaDang(pokers, false);
			for(let i = 0; i < zhadans.length; i++){
				let zhadan = zhadans[i]
				if(this.isHaveGui(guipai, zhadan)) continue;
				zhadan.push(guipai[0]);
				wutongs[wutongs.length] = zhadan;
			}
		}
		else if(guipai.length > 1){
			let santiaos = this.GetSanTiao(pokers, false);
			for(let i = 0; i < santiaos.length; i++){
				let santiao = santiaos[i]
				if(this.isHaveGui(guipai, santiao)) continue;
				for(let j = 0; j < guipai.length; j++){
					santiao.push(guipai[j]);
				}
				wutongs[wutongs.length] = santiao;
			}
		}
		console.log('五同 :', wutongs);
		return wutongs;*/
	},
//检查有没有大小鬼
	isHaveGui: function (guipai, list) {
		let isHave = false;
		/*for (let j = 0; j < list.length; j++) {
			if (guipai.indexOf(list[j]) != -1) {
				isHave = true;
				break;
			}
		}*/
		return isHave;
	},

///////////////////////////common///////////////////////////////////////
	//检查是否是同一花色
	CheckSameColor: function (list) {
		if (list.length == 0) return false;
		let guipai = this.GetGuiPai(list);
		if (guipai.length >= 4) return true;
		let bSameColor = true;
		let startColor = -1;
		for (let j = 0; j < list.length; j++) {
			if (guipai.indexOf(list[j]) != -1) continue;
			startColor = this.GetCardColor(list[j]);
			break;
		}

		for (let i = 0; i < list.length; i++) {
			if (guipai.indexOf(list[i]) != -1) continue;
			let color = this.GetCardColor(list[i]);
			if (color != startColor) {
				bSameColor = false;
				break;
			}
		}
		return bSameColor;
	},
	CheckPokerInList: function (list, tagCard) {
		if (list.length == 0) return false;

		let bInList = false;
		for (let i = 0; i < list.length; i++) {
			let item = list[i];
			let pos = item.indexOf(tagCard);


			if (pos >= 0) {
				bInList = true;
			}
		}
		return bInList
	},

	GetContinueValue: function (pokers, tagCard) {
		if (pokers.length == 0) return;
		let continueValueList = [];
		let tagCardValue = this.GetCardValue(tagCard);
		let continueTime = 1

		for (let i = 0; i < pokers.length; i++) {
			let poker = pokers[i];
			let continueValue = this.GetCardValue(poker);

			if (tagCardValue + continueTime == continueValue) {
				continueValueList[continueValueList.length] = poker;
				continueTime++;
			}
		}
		return continueValueList
	},

	//获取下一牌值
	GetNextValue: function (pokers, tagCard) {
		if (list.length == 0) return;
		let nextValueList = [];
		let tagCardValue = this.GetCardValue(tagCard);
		for (let i = 0; i < pokers.length; i++) {
			let poker = pokers[i];
			let nextValue = this.GetCardValue(poker);

			if (tagCardValue + 1 == nextValue) {
				nextValueList[nextValueList.length] = poker;
			}
		}
		return nextValueList
	},

	//获取同一牌值
	GetSameValue: function (pokers, tagCard) {
		let sameValueList = [];
		let tagCardValue = this.GetCardValue(tagCard);
		for (let i = 0; i < pokers.length; i++) {
			let poker = pokers[i];
			let pokerValue = this.GetCardValue(poker);

			if (tagCardValue == pokerValue) {
				sameValueList[sameValueList.length] = poker;
			}
		}
		return sameValueList
	},

	//获取同一花色
	GetSameColor: function (pokers, tagCard) {
		let sameColorList = [];
		for (let i = 0; i < pokers.length; i++) {
			let poker = pokers[i];
			let pokerColor = this.GetCardColor(poker);
			let tagCardColor = this.GetCardColor(tagCard);

			if (pokerColor == tagCardColor) {
				sameColorList[sameColorList.length] = poker;
			}
		}
		return sameColorList;
	},

	//获取牌值
	GetCardValue: function (poker) {
		console.log('=============LZBJPKLogicGame GetCardValue==============');
		let newPoker = this.PokerCard.SubCardValue(poker);
		if (newPoker == this.Logic_xiaowang) {     //小王65  大小王是癞子，直接是最小值
			return 0;  
		} else if (newPoker == this.Logic_dawang) {//大王66
			return 1;
		}
		return newPoker & this.LOGIC_MASK_VALUE;
	},

	//获取花色
	GetCardColor: function (poker) {
		console.log('=============LZBJPKLogicGame GetCardColor==============');
		let newPoker = this.PokerCard.SubCardValue(poker);
		if(newPoker === this.Logic_dawang){
			return -1;  
		}else if(newPoker === this.Logic_dawang){
			return -2;
		}
		return newPoker & this.LOGIC_MASK_COLOR;
	},

	GetSortCards: function (pokers) {
		if (!pokers.length) return;
		let guipai = this.GetGuiPai(pokers);
		let array = [];

		for (let idx = 0; idx < pokers.length; idx++) {
			let poker = pokers[idx];
			if (guipai.indexOf(poker) != -1) continue;
			let data = {};
			data.cardValue = this.GetCardValue(poker);
			data.cardX16 = poker;
			array.push(data);
		}

		array.sort(function (a, b) {
			return b.cardValue - a.cardValue;
		});

		//鬼牌不进行排序 向前插入
		for (let i = 0; i < guipai.length; i++) {
			let data = {};
			data.cardValue = this.GetCardValue(guipai[i]);
			data.cardX16 = guipai[i];
			array.unshift(data);
		}

		return array;
	},
	SortCardsByColor: function (pokers) {
		if (!pokers.length) {
			return;
		}
		let array = [];
		for (let idx = 0; idx < pokers.length; idx++) {
			let poker = pokers[idx];
			let data = {};
			data.cardValue = this.GetCardValue(poker);
			data.cardColor = this.GetCardColor(poker);
			data.cardX16 = poker;
			array.push(data);
		}
		array.sort((a, b) => {
			return b.cardColor - a.cardColor;
		});
		return array;
	},

	/**
	 * 按花色排
	 * @param {*} pokers 
	 * @param {*} sortType 
	 * @returns 
	 */
	GetSortCardsByColor: function (pokers, sortType) {
		if (!pokers.length) {
			return;
		}

		let array = [];
		let self = this;
		for (let idx = 0; idx < pokers.length; idx++) {
			let poker = pokers[idx];
			let data = {};
			data.cardValue = this.GetCardValue(poker);
			data.cardColor = this.GetCardColor(poker);
			data.cardX16 = poker;
			array.push(data);
		}
		array.sort(function (a, b) {
			if (a.cardColor == b.cardColor) {
				return b.cardValue - a.cardValue;
			} else {
				if (sortType == 1) {
					return b.cardColor - a.cardColor;
				} else if (sortType == 2) {
					return a.cardColor - b.cardColor;
				}
			}
		});
		return array;
	},

	/**
	 * 按大小排序
	 * @param {*} pokers 牌数
	 * @param {*} sortType 升降序
	 * @returns 
	 */
	GetSortCardsByMax: function (pokers, sortType) {
		if (!pokers.length) {
			return;
		}

		let array = [];
		let self = this;
		for (let idx = 0; idx < pokers.length; idx++) {
			let poker = pokers[idx];
			let data = {};
			data.cardValue = this.GetCardValue(poker);
			data.cardColor = this.GetCardColor(poker);
			data.cardX16 = poker;
			array.push(data);
		}
		array.sort(function (a, b) {
			if (sortType == 1) {
				return b.cardValue - a.cardValue;
			} else if (sortType == 2) {
				return a.cardValue - b.cardValue;
			}
		});
		return array;
	},

	CheckSameValue: function (aCards, bCards) {
		let bRet = false;
		for (let i = 0; i < aCards.length; i++) {
			let poker = aCards[i];
			if (bCards.indexOf(poker) != -1) {
				bRet = true;
				break;
			}
		}

		return bRet;
	},
	PushTipCard: function (pokers, samePoker, len) {
		let temp = [];
		samePoker.reverse();
		this.SortCardByMin(samePoker);
		for (let i = 0; i < len; i++) {
			temp.push(samePoker[i]);
		}
		pokers.push(temp);
	},
	CheckPokerInListEx: function (list, tagCard) {
		if (list.length == 0) {
			return false;
		}

		let bInList = false;
		let tagValue = this.GetCardValue(tagCard);
		for (let i = 0; i < list.length; i++) {
			let item = list[i];
			for (let j = 0; j < item.length; j++) {
				let cardValue = this.GetCardValue(item[j]);
				if (cardValue == tagValue) {
					return true;
				}
			}
		}
		return bInList;
	},
});

var g_LogicBJPKGame = null;

/**
 * 绑定模块外部方法
 */
exports.GetModel = function () {
	if (!g_LogicBJPKGame) {
		g_LogicBJPKGame = new LogicBJPKGame();
	}
	return g_LogicBJPKGame;

}